package com.bnivideo.ui.reflection

/**
 * Created by IntelliJ IDEA.
 * User: jiming
 * Date: 11-7-29
 * Time: 下午2:13
 * To change this template use File | Settings | File Templates.
 */
class MockBeanCreator {
    def ascii = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    
    public String process(String className, Map<String, String> fields) {
        println "public static $className createRandom(Random r) {"
        println "    $className result = new $className();\n"
        if(fields.values().contains('char') || fields.values().contains('Character')) {
            println "    final String ASCII_CHARS = \"$ascii\"; ";
        }
        fields.each {k, v ->
            println "    " + processField(k, v);
        }
        println "\n\treturn result;"
        println "}"
    }

    protected String processField(String field_name, String field_type) {
        if(field_name.indexOf('_') != -1) {
            System.err.println("Having underline in field name is not good. [$field_name]");
        }
        if("boolean".equalsIgnoreCase(field_type)) {
            return "result.set" + field_name.capitalize() + "(r.nextInt(2)==0);"
        } else if("char".equalsIgnoreCase(field_type) || "Character".equalsIgnoreCase(field_type)) {
            return "result.set" + field_name.capitalize() + "(ASCII_CHARS.charAt(r.nextInt(ASCII_CHARS.length())));"
        } else if("byte".equalsIgnoreCase(field_type)) {
            return "result.set" + field_name.capitalize() + "((byte)r.nextInt(Byte.MAX_VALUE));"
        } else if("short".equalsIgnoreCase(field_type)) {
            return "result.set" + field_name.capitalize() + "((short)r.nextInt(Short.MAX_VALUE));"
        } else if("int".equalsIgnoreCase(field_type) ||"integer".equalsIgnoreCase(field_type) ) {
            return "result.set" + field_name.capitalize() + "(r.nextInt());"
        } else if("long".equalsIgnoreCase(field_type)) {
            return "result.set" + field_name.capitalize() + "((long)r.nextInt());"
        } else if("float".equalsIgnoreCase(field_type)) {
            return "result.set" + field_name.capitalize() + "((float)r.nextInt());"
        } else if("double".equalsIgnoreCase(field_type)) {
            return "result.set" + field_name.capitalize() + "((double)r.nextInt());"
        } else if("String".equalsIgnoreCase(field_type)) {
            return "result.set" + field_name.capitalize() + "(\"${field_name}-\" + r.nextInt());"
        } else if("Date".equalsIgnoreCase(field_type)) {
            return "result.set" + field_name.capitalize() + "(new Date(System.currentTimeMillis() - r.nextInt()));"
        } else if("Timestamp".equalsIgnoreCase(field_type)) {
            return "result.set" + field_name.capitalize() + "(new Timestamp(System.currentTimeMillis() - r.nextInt()));"
        } else if("set".equalsIgnoreCase(field_type)) {
            return "// Field ["+field_name + ":" + field_type+"] not supported.";
        } else if("map".equalsIgnoreCase(field_type)) {
            return "// Field ["+field_name + ":" + field_type+"] not supported.";
        } else if("collection".equalsIgnoreCase(field_type)) {
            return "// Field ["+field_name + ":" + field_type+"] not supported.";
        } else if("list".equalsIgnoreCase(field_type)) {
            return "// Field ["+field_name + ":" + field_type+"] not supported.";
        } else if("class".equalsIgnoreCase(field_type)) {
            return "// Field ["+field_name + ":" + field_type+"] not supported.";
        } else if("object".equalsIgnoreCase(field_type)) {
            return "// Field ["+field_name + ":" + field_type+"] not supported.";
        } else {
            // treat it as Enum
            return "result.set" + field_name.capitalize() + "(${field_type}.values()[r.nextInt(${field_type}.values().length)]);"
        }
    }


}
